//  工具函数库

export default {
  /**
   * 两数相加方法，解决  0.1 + 0.2 = 0.30000000000000004
   * @param arg1
   * @param arg2
   * @returns {number}
   */
  accAdd: function (arg1, arg2) {
    var r1, r2, m;

    try {
      r1 = arg1.toString().split(".")[1].length;
    } catch (e) {
      r1 = 0;
    }
    try {
      r2 = arg2.toString().split(".")[1].length;
    } catch (e) {
      r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2));

    return (arg1 * m + arg2 * m) / m;
  },
  /**
   * param 将要转为URL参数字符串的对象
   * key URL参数字符串的前缀
   * encode true/false 是否进行URL编码,默认为true
   *
   * return URL参数字符串
   */
  urlEncode: function (param, key, encode) {
    if (param == null) {
      return '';
    }

    var paramStr = '';
    var t = typeof (param);

    if (t == 'string' || t == 'number' || t == 'boolean') {
      paramStr += '&' + key + '=' + ((encode == null || encode) ? encodeURIComponent(param) : param);
    } else {
      for (var i in param) {
        var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i);
        paramStr += this.urlEncode(param[i], k, encode);
      }
    }

    return paramStr;
  },
  getSexByID: function(id) {
    let sexno;

    if(id.length == 18) {
      sexno = id.substring(16, 17)
    } else if(id.length == 15) {
      sexno = id.substring(14, 15)
    }

    sexno = sexno % 2;
    if(sexno == 0) {
      return '女'
    } else {
      return '男'
    }
  },
  getBirthdayByID: function(idCard) {
    var birthday = "";

    if (idCard != null && idCard != "") {
      if (idCard.length == 15) {
        birthday = "19"+ idCard.substr(6,6);
      } else if (idCard.length == 18) {
        birthday = idCard.substr(6,8);
      }

      birthday = birthday.replace(/(.{4})(.{2})/,"$1-$2-");
    }

    return birthday;

  },
  getAgeByID: function(id) {
    let birthdayno, birthdaytemp

    if(id.length == 18) {
      birthdayno = id.substring(6, 10) //  获取年份
    } else if(id.length == 15) {
      birthdaytemp = id.substring(6, 8)
      birthdayno = "19" + birthdaytemp
    }

    let age = new Date().getFullYear() - birthdayno

    return age
  },
  /**
   * 字符串转对象
   * @param str
   * @param splitStr  分隔符
   * @returns {{}}
   *
   * str example：a=1&b=2
   * 函数没有做过多判断校验，请确保str的有效性
   */
  getObjByStr: function (str, splitStr = '&') {
    if (!str) {
      return {}
    }

    str = decodeURIComponent(str)
    let arr = str.split(splitStr)

    if (!arr.length) {
      return {}
    }

    let obj = {}
    arr.forEach(item => {
      let itemArr = item.split('=')

      if (itemArr.length) {
        obj[itemArr[0]] = itemArr[1]
      }
    })

    return obj
  },
  /**
   * 对象转查询字符串
   * @param obj
   * @param splitStr
   * @returns {string}
   */
  getStrByObj: function (obj, splitStr = '&') {
    if (!obj) {
      return ''
    }

    let arr = []
    for (let i in obj) {
      if (!obj.hasOwnProperty(i)) {
        continue
      }
      arr.push(`${i}=${obj[i]}`)
    }

    return arr.join(splitStr)
  },
  /**
   * 手机号加密
   * @param mobile
   * @returns {*}
   */
  secretMobile: function (mobile) {
    try {
      return mobile.replace(/^(\d{3})\d{4}(\d+)/,"$1****$2");
    } catch (err) {
      return mobile
    }
  },
}
